package com.fafu.rbac.security;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fafu.rbac.domain.entity.User;
import com.fafu.rbac.security.bean.WegoUserDetails;
import com.fafu.rbac.service.UserService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Set;

@Service
public class WegoUserDetailsService implements UserDetailsService {

    @Resource
    private UserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userService.getOne(Wrappers.<User>query().eq("username", username));
        //TODO:查询权限信息

        //查询数据库获取用户的角色+权限
        Set<String> rolePermission = userService.getRolePermission(user.getId());

        WegoUserDetails userDetails = new WegoUserDetails();
        userDetails.setUser(user);
        userDetails.setRolePerssion(rolePermission);

        return userDetails;
    }

}
